home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / mixer.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  26KB  |  884 lines

  1. /***************************************************************************
  2.  
  3.   mixer.c
  4.  
  5.   Manage audio channels allocation, with volume and panning control
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include <math.h>
  11.  
  12.  
  13. /* enable this to turn off clipping (helpful to find cases where we max out */
  14. #define DISABLE_CLIPPING        0
  15.  
  16. /* accumulators have ACCUMULATOR_SAMPLES samples (must be a power of 2) */
  17. #define ACCUMULATOR_SAMPLES        8192
  18. #define ACCUMULATOR_MASK        (ACCUMULATOR_SAMPLES - 1)
  19.  
  20. /* fractional numbers have FRACTION_BITS bits of resolution */
  21. #define FRACTION_BITS            16
  22. #define FRACTION_MASK            ((1 << FRACTION_BITS) - 1)
  23.  
  24.  
  25. static int mixer_sound_enabled;
  26.  
  27.  
  28. /* holds all the data for the a mixer channel */
  29. struct mixer_channel_data
  30. {
  31.     char        name[40];
  32.  
  33.     /* current volume, gain and pan */
  34.     INT32        volume;
  35.     INT32        gain;
  36.     INT32        pan;
  37.  
  38.     /* mixing levels */
  39.     UINT8        mixing_level;
  40.     UINT8        default_mixing_level;
  41.     UINT8        config_mixing_level;
  42.     UINT8        config_default_mixing_level;
  43.  
  44.     /* current playback positions */
  45.     UINT32        input_frac;
  46.     UINT32        samples_available;
  47.     UINT32        frequency;
  48.     UINT32        step_size;
  49.  
  50.     /* state of non-streamed playback */
  51.     UINT8        is_stream;
  52.     UINT8        is_playing;
  53.     UINT8        is_looping;
  54.     UINT8        is_16bit;
  55.     void *        data_start;
  56.     void *        data_end;
  57.     void *        data_current;
  58. };
  59.  
  60.  
  61.  
  62. /* channel data */
  63. static struct mixer_channel_data mixer_channel[MIXER_MAX_CHANNELS];
  64. static UINT8 config_mixing_level[MIXER_MAX_CHANNELS];
  65. static UINT8 config_default_mixing_level[MIXER_MAX_CHANNELS];
  66. static UINT8 first_free_channel = 0;
  67. static UINT8 config_invalid;
  68. static UINT8 is_stereo;
  69.  
  70. /* 32-bit accumulators */
  71. static UINT32 accum_base;
  72. static INT32 left_accum[ACCUMULATOR_SAMPLES];
  73. static INT32 right_accum[ACCUMULATOR_SAMPLES];
  74.  
  75. /* 16-bit mix buffers */
  76. static INT16 mix_buffer[ACCUMULATOR_SAMPLES*2];    /* *2 for stereo */
  77.  
  78. /* global sample tracking */
  79. static UINT32 samples_this_frame;
  80.  
  81.  
  82.  
  83. /* function prototypes */
  84. static void mix_sample_8(struct mixer_channel_data *channel, int samples_to_generate);
  85. static void mix_sample_16(struct mixer_channel_data *channel, int samples_to_generate);
  86.  
  87.  
  88.  
  89. /***************************************************************************
  90.     mixer_sh_start
  91. ***************************************************************************/
  92.  
  93. int mixer_sh_start(void)
  94. {
  95.     struct mixer_channel_data *channel;
  96.     int i;
  97.  
  98.     /* reset all channels to their defaults */
  99.     memset(&mixer_channel, 0, sizeof(mixer_channel));
  100.     for (i = 0, channel = mixer_channel; i < MIXER_MAX_CHANNELS; i++, channel++)
  101.     {
  102.         channel->mixing_level                     = 0xff;
  103.         channel->default_mixing_level             = 0xff;
  104.         channel->config_mixing_level             = config_mixing_level[i];
  105.         channel->config_default_mixing_level     = config_default_mixing_level[i];
  106.     }
  107.  
  108.     /* determine if we're playing in stereo or not */
  109.     first_free_channel = 0;
  110.     is_stereo = ((Machine->drv->sound_attributes & SOUND_SUPPORTS_STEREO) != 0);
  111.  
  112.     /* clear the accumulators */
  113.     accum_base = 0;
  114.     memset(left_accum, 0, ACCUMULATOR_SAMPLES * sizeof(INT32));
  115.     memset(right_accum, 0, ACCUMULATOR_SAMPLES * sizeof(INT32));
  116.  
  117.     samples_this_frame = osd_start_audio_stream(is_stereo);
  118.  
  119.     mixer_sound_enabled = 1;
  120.  
  121.     return 0;
  122. }
  123.  
  124.  
  125. /***************************************************************************
  126.     mixer_sh_stop
  127. ***************************************************************************/
  128.  
  129. void mixer_sh_stop(void)
  130. {
  131.     osd_stop_audio_stream();
  132. }
  133.  
  134.  
  135. /***************************************************************************
  136.     mixer_update_channel
  137. ***************************************************************************/
  138.  
  139. void mixer_update_channel(struct mixer_channel_data *channel, int total_sample_count)
  140. {
  141.     int samples_to_generate = total_sample_count - channel->samples_available;
  142.  
  143.     /* don't do anything for streaming channels */
  144.     if (channel->is_stream)
  145.         return;
  146.  
  147.     /* if we're all caught up, just return */
  148.     if (samples_to_generate <= 0)
  149.         return;
  150.  
  151.     /* if we're playing, mix in the data */
  152.     if (channel->is_playing)
  153.     {
  154.         if (channel->is_16bit)
  155.             mix_sample_16(channel, samples_to_generate);
  156.         else
  157.             mix_sample_8(channel, samples_to_generate);
  158.     }
  159.  
  160.     /* just eat the rest */
  161.     channel->samples_available += samples_to_generate;
  162. }
  163.  
  164.  
  165. /***************************************************************************
  166.     mixer_sh_update
  167. ***************************************************************************/
  168.  
  169. void mixer_sh_update(void)
  170. {
  171.     struct mixer_channel_data *    channel;
  172.     UINT32 accum_pos = accum_base;
  173.     INT16 *mix;
  174.     INT32 sample;
  175.     int    i;
  176.  
  177.     profiler_mark(PROFILER_MIXER);
  178.  
  179.     /* update all channels (for streams this is a no-op) */
  180.     for (i = 0, channel = mixer_channel; i < first_free_channel; i++, channel++)
  181.     {
  182.         mixer_update_channel(channel, samples_this_frame);
  183.  
  184.         /* if we needed more than they could give, adjust their pointers */
  185.         if (samples_this_frame > channel->samples_available)
  186.             channel->samples_available = 0;
  187.         else
  188.             channel->samples_available -= samples_this_frame;
  189.     }
  190.  
  191.     /* copy the mono 32-bit data to a 16-bit buffer, clipping along the way */
  192.     if (!is_stereo)
  193.     {
  194.         mix = mix_buffer;
  195.         for (i = 0; i < samples_this_frame; i++)
  196.         {
  197.             /* fetch and clip the sample */
  198.             sample = left_accum[accum_pos];
  199. #if !DISABLE_CLIPPING
  200.             if (sample < -32768)
  201.                 sample = -32768;
  202.             else if (sample > 32767)
  203.                 sample = 32767;
  204. #endif
  205.  
  206.             /* store and zero out behind us */
  207.             *mix++ = sample;
  208.             left_accum[accum_pos] = 0;
  209.  
  210.             /* advance to the next sample */
  211.             accum_pos = (accum_pos + 1) & ACCUMULATOR_MASK;
  212.         }
  213.     }
  214.  
  215.     /* copy the stereo 32-bit data to a 16-bit buffer, clipping along the way */
  216.     else
  217.     {
  218.         mix = mix_buffer;
  219.         for (i = 0; i < samples_this_frame; i++)
  220.         {
  221.             /* fetch and clip the left sample */
  222.             sample = left_accum[accum_pos];
  223. #if !DISABLE_CLIPPING
  224.             if (sample < -32768)
  225.                 sample = -32768;
  226.             else if (sample > 32767)
  227.                 sample = 32767;
  228. #endif
  229.  
  230.             /* store and zero out behind us */
  231.             *mix++ = sample;
  232.             left_accum[accum_pos] = 0;
  233.  
  234.             /* fetch and clip the right sample */
  235.             sample = right_accum[accum_pos];
  236. #if !DISABLE_CLIPPING
  237.             if (sample < -32768)
  238.                 sample = -32768;
  239.             else if (sample > 32767)
  240.                 sample = 32767;
  241. #endif
  242.  
  243.             /* store and zero out behind us */
  244.             *mix++ = sample;
  245.             right_accum[accum_pos] = 0;
  246.  
  247.             /* advance to the next sample */
  248.             accum_pos = (accum_pos + 1) & ACCUMULATOR_MASK;
  249.         }
  250.     }
  251.  
  252.     /* play the result */
  253.     samples_this_frame = osd_update_audio_stream(mix_buffer);
  254.  
  255.     accum_base = accum_pos;
  256.  
  257.     profiler_mark(PROFILER_END);
  258. }
  259.  
  260.  
  261. /***************************************************************************
  262.     mixer_allocate_channel
  263. ***************************************************************************/
  264.  
  265. int mixer_allocate_channel(int default_mixing_level)
  266. {
  267.     /* this is just a degenerate case of the multi-channel mixer allocate */
  268.     return mixer_allocate_channels(1, &default_mixing_level);
  269. }
  270.  
  271.  
  272. /***************************************************************************
  273.     mixer_allocate_channels
  274. ***************************************************************************/
  275.  
  276. int mixer_allocate_channels(int channels, const int *default_mixing_levels)
  277. {
  278.     int i, j;
  279.  
  280.     /* make sure we didn't overrun the number of available channels */
  281.     if (first_free_channel + channels > MIXER_MAX_CHANNELS)
  282.     {
  283.         logerror("Too many mixer channels (requested %d, available %d)\n", first_free_channel + channels, MIXER_MAX_CHANNELS);
  284.         exit(1);
  285.     }
  286.  
  287.     /* loop over channels requested */
  288.     for (i = 0; i < channels; i++)
  289.     {
  290.         struct mixer_channel_data *channel = &mixer_channel[first_free_channel + i];
  291.  
  292.         /* extract the basic data */
  293.         channel->default_mixing_level     = MIXER_GET_LEVEL(default_mixing_levels[i]);
  294.         channel->pan                     = MIXER_GET_PAN(default_mixing_levels[i]);
  295.         channel->gain                     = MIXER_GET_GAIN(default_mixing_levels[i]);
  296.         channel->volume                 = 100;
  297.  
  298.         /* backwards compatibility with old 0-255 volume range */
  299.         if (channel->default_mixing_level > 100)
  300.             channel->default_mixing_level = channel->default_mixing_level * 25 / 255;
  301.  
  302.         /* attempt to load in the configuration data for this channel */
  303.         channel->mixing_level = channel->default_mixing_level;
  304.         if (!config_invalid)
  305.         {
  306.             /* if the defaults match, set the mixing level from the config */
  307.             if (channel->default_mixing_level == channel->config_default_mixing_level)
  308.                 channel->mixing_level = channel->config_mixing_level;
  309.  
  310.             /* otherwise, invalidate all channels that have been created so far */
  311.             else
  312.             {
  313.                 config_invalid = 1;
  314.                 for (j = 0; j < first_free_channel + i; j++)
  315.                     mixer_set_mixing_level(j, mixer_channel[j].default_mixing_level);
  316.             }
  317.         }
  318.  
  319.         /* set the default name */
  320.         mixer_set_name(first_free_channel + i, 0);
  321.     }
  322.  
  323.     /* increment the counter and return the first one */
  324.     first_free_channel += channels;
  325.     return first_free_channel - channels;
  326. }
  327.  
  328.  
  329. /***************************************************************************
  330.     mixer_set_name
  331. ***************************************************************************/
  332.  
  333. void mixer_set_name(int ch, const char *name)
  334. {
  335.     struct mixer_channel_data *channel = &mixer_channel[ch];
  336.  
  337.     /* either copy the name or create a default one */
  338.     if (name != NULL)
  339.         strcpy(channel->name, name);
  340.     else
  341.         sprintf(channel->name, "<channel #%d>", ch);
  342.  
  343.     /* append left/right onto the channel as appropriate */
  344.     if (channel->pan == MIXER_PAN_LEFT)
  345.         strcat(channel->name, " (Lt)");
  346.     else if (channel->pan == MIXER_PAN_RIGHT)
  347.         strcat(channel->name, " (Rt)");
  348. }
  349.  
  350.  
  351. /***************************************************************************
  352.     mixer_get_name
  353. ***************************************************************************/
  354.  
  355. const char *mixer_get_name(int ch)
  356. {
  357.     struct mixer_channel_data *channel = &mixer_channel[ch];
  358.  
  359.     /* return a pointer to the name or a NULL for an unused channel */
  360.     if (channel->name[0] != 0)
  361.         return channel->name;
  362.     else
  363.         return NULL;
  364. }
  365.  
  366.  
  367. /***************************************************************************
  368.     mixer_set_volume
  369. ***************************************************************************/
  370.  
  371. void mixer_set_volume(int ch, int volume)
  372. {
  373.     struct mixer_channel_data *channel = &mixer_channel[ch];
  374.  
  375.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  376.     channel->volume = volume;
  377. }
  378.  
  379.  
  380. /***************************************************************************
  381.     mixer_set_mixing_level
  382. ***************************************************************************/
  383.  
  384. void mixer_set_mixing_level(int ch, int level)
  385. {
  386.     struct mixer_channel_data *channel = &mixer_channel[ch];
  387.  
  388.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  389.     channel->mixing_level = level;
  390. }
  391.  
  392.  
  393. /***************************************************************************
  394.     mixer_get_mixing_level
  395. ***************************************************************************/
  396.  
  397. int mixer_get_mixing_level(int ch)
  398. {
  399.     struct mixer_channel_data *channel = &mixer_channel[ch];
  400.     return channel->mixing_level;
  401. }
  402.  
  403.  
  404. /***************************************************************************
  405.     mixer_get_default_mixing_level
  406. ***************************************************************************/
  407.  
  408. int mixer_get_default_mixing_level(int ch)
  409. {
  410.     struct mixer_channel_data *channel = &mixer_channel[ch];
  411.     return channel->default_mixing_level;
  412. }
  413.  
  414.  
  415. /***************************************************************************
  416.     mixer_read_config
  417. ***************************************************************************/
  418.  
  419. void mixer_read_config(void *f)
  420. {
  421.     UINT8 default_levels[MIXER_MAX_CHANNELS];
  422.     UINT8 mixing_levels[MIXER_MAX_CHANNELS];
  423.     int i;
  424.  
  425.     memset(default_levels, 0xff, sizeof(default_levels));
  426.     memset(mixing_levels, 0xff, sizeof(mixing_levels));
  427.     osd_fread(f, default_levels, MIXER_MAX_CHANNELS);
  428.     osd_fread(f, mixing_levels, MIXER_MAX_CHANNELS);
  429.     for (i = 0; i < MIXER_MAX_CHANNELS; i++)
  430.     {
  431.         config_default_mixing_level[i] = default_levels[i];
  432.         config_mixing_level[i] = mixing_levels[i];
  433.     }
  434.     config_invalid = 0;
  435. }
  436.  
  437.  
  438. /***************************************************************************
  439.     mixer_write_config
  440. ***************************************************************************/
  441.  
  442. void mixer_write_config(void *f)
  443. {
  444.     UINT8 default_levels[MIXER_MAX_CHANNELS];
  445.     UINT8 mixing_levels[MIXER_MAX_CHANNELS];
  446.     int i;
  447.  
  448.     for (i = 0; i < MIXER_MAX_CHANNELS; i++)
  449.     {
  450.         default_levels[i] = mixer_channel[i].default_mixing_level;
  451.         mixing_levels[i] = mixer_channel[i].mixing_level;
  452.     }
  453.     osd_fwrite(f, default_levels, MIXER_MAX_CHANNELS);
  454.     osd_fwrite(f, mixing_levels, MIXER_MAX_CHANNELS);
  455. }
  456.  
  457.  
  458. /***************************************************************************
  459.     mixer_play_streamed_sample_16
  460. ***************************************************************************/
  461.  
  462. void mixer_play_streamed_sample_16(int ch, INT16 *data, int len, int freq)
  463. {
  464.     struct mixer_channel_data *channel = &mixer_channel[ch];
  465.     UINT32 step_size, input_pos, output_pos, samples_mixed;
  466.     INT32 mixing_volume;
  467.  
  468.     /* skip if sound is off */
  469.     if (Machine->sample_rate == 0)
  470.         return;
  471.     channel->is_stream = 1;
  472.  
  473.     profiler_mark(PROFILER_MIXER);
  474.  
  475.     /* compute the overall mixing volume */
  476.     if (mixer_sound_enabled)
  477.         mixing_volume = ((channel->volume * channel->mixing_level * 256) << channel->gain) / (100*100);
  478.     else
  479.         mixing_volume = 0;
  480.  
  481.     /* compute the step size for sample rate conversion */
  482.     if (freq != channel->frequency)
  483.     {
  484.         channel->frequency = freq;
  485.         channel->step_size = (UINT32)((double)freq * (double)(1 << FRACTION_BITS) / (double)Machine->sample_rate);
  486.     }
  487.     step_size = channel->step_size;
  488.  
  489.     /* now determine where to mix it */
  490.     input_pos = channel->input_frac;
  491.     output_pos = (accum_base + channel->samples_available) & ACCUMULATOR_MASK;
  492.  
  493.     /* compute the length in fractional form */
  494.     len = (len / 2) << FRACTION_BITS;
  495.     samples_mixed = 0;
  496.  
  497.     /* if we're mono or left panning, just mix to the left channel */
  498.     if (!is_stereo || channel->pan == MIXER_PAN_LEFT)
  499.     {
  500.         while (input_pos < len)
  501.         {
  502.             left_accum[output_pos] += (data[input_pos >> FRACTION_BITS] * mixing_volume) >> 8;
  503.             input_pos += step_size;
  504.             output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  505.             samples_mixed++;
  506.         }
  507.     }
  508.  
  509.     /* if we're right panning, just mix to the right channel */
  510.     else if (channel->pan == MIXER_PAN_RIGHT)
  511.     {
  512.         while (input_pos < len)
  513.         {
  514.             right_accum[output_pos] += (data[input_pos >> FRACTION_BITS] * mixing_volume) >> 8;
  515.             input_pos += step_size;
  516.             output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  517.             samples_mixed++;
  518.         }
  519.     }
  520.  
  521.     /* if we're stereo center, mix to both channels */
  522.     else
  523.     {
  524.         while (input_pos < len)
  525.         {
  526.             INT32 mixing_value = (data[input_pos >> FRACTION_BITS] * mixing_volume) >> 8;
  527.             left_accum[output_pos] += mixing_value;
  528.             right_accum[output_pos] += mixing_value;
  529.             input_pos += step_size;
  530.             output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  531.             samples_mixed++;
  532.         }
  533.     }
  534.  
  535.     /* update the final positions */
  536.     channel->input_frac = input_pos & FRACTION_MASK;
  537.     channel->samples_available += samples_mixed;
  538.  
  539.     profiler_mark(PROFILER_END);
  540. }
  541.  
  542.  
  543. /***************************************************************************
  544.     mixer_samples_this_frame
  545. ***************************************************************************/
  546.  
  547. int mixer_samples_this_frame(void)
  548. {
  549.     return samples_this_frame;
  550. }
  551.  
  552.  
  553. /***************************************************************************
  554.     mixer_need_samples_this_frame
  555. ***************************************************************************/
  556. #define EXTRA_SAMPLES 1    // safety margin for sampling rate conversion
  557. int mixer_need_samples_this_frame(int channel,int freq)
  558. {
  559.     return (samples_this_frame - mixer_channel[channel].samples_available)
  560.             * freq / Machine->sample_rate + EXTRA_SAMPLES;
  561. }
  562.  
  563.  
  564. /***************************************************************************
  565.     mixer_play_sample
  566. ***************************************************************************/
  567.  
  568. void mixer_play_sample(int ch, INT8 *data, int len, int freq, int loop)
  569. {
  570.     struct mixer_channel_data *channel = &mixer_channel[ch];
  571.  
  572.     /* skip if sound is off, or if this channel is a stream */
  573.     if (Machine->sample_rate == 0 || channel->is_stream)
  574.         return;
  575.  
  576.     /* update the state of this channel */
  577.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  578.  
  579.     /* compute the step size for sample rate conversion */
  580.     if (freq != channel->frequency)
  581.     {
  582.         channel->frequency = freq;
  583.         channel->step_size = (UINT32)((double)freq * (double)(1 << FRACTION_BITS) / (double)Machine->sample_rate);
  584.     }
  585.  
  586.     /* now determine where to mix it */
  587.     channel->input_frac = 0;
  588.     channel->data_start = data;
  589.     channel->data_current = data;
  590.     channel->data_end = (UINT8 *)data + len;
  591.     channel->is_playing = 1;
  592.     channel->is_looping = loop;
  593.     channel->is_16bit = 0;
  594. }
  595.  
  596.  
  597. /***************************************************************************
  598.     mixer_play_sample_16
  599. ***************************************************************************/
  600.  
  601. void mixer_play_sample_16(int ch, INT16 *data, int len, int freq, int loop)
  602. {
  603.     struct mixer_channel_data *channel = &mixer_channel[ch];
  604.  
  605.     /* skip if sound is off, or if this channel is a stream */
  606.     if (Machine->sample_rate == 0 || channel->is_stream)
  607.         return;
  608.  
  609.     /* update the state of this channel */
  610.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  611.  
  612.     /* compute the step size for sample rate conversion */
  613.     if (freq != channel->frequency)
  614.     {
  615.         channel->frequency = freq;
  616.         channel->step_size = (UINT32)((double)freq * (double)(1 << FRACTION_BITS) / (double)Machine->sample_rate);
  617.     }
  618.  
  619.     /* now determine where to mix it */
  620.     channel->input_frac = 0;
  621.     channel->data_start = data;
  622.     channel->data_current = data;
  623.     channel->data_end = (UINT8 *)data + len;
  624.     channel->is_playing = 1;
  625.     channel->is_looping = loop;
  626.     channel->is_16bit = 1;
  627. }
  628.  
  629.  
  630. /***************************************************************************
  631.     mixer_stop_sample
  632. ***************************************************************************/
  633.  
  634. void mixer_stop_sample(int ch)
  635. {
  636.     struct mixer_channel_data *channel = &mixer_channel[ch];
  637.  
  638.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  639.     channel->is_playing = 0;
  640. }
  641.  
  642.  
  643. /***************************************************************************
  644.     mixer_is_sample_playing
  645. ***************************************************************************/
  646.  
  647. int mixer_is_sample_playing(int ch)
  648. {
  649.     struct mixer_channel_data *channel = &mixer_channel[ch];
  650.  
  651.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  652.     return channel->is_playing;
  653. }
  654.  
  655.  
  656. /***************************************************************************
  657.     mixer_set_sample_frequency
  658. ***************************************************************************/
  659.  
  660. void mixer_set_sample_frequency(int ch, int freq)
  661. {
  662.     struct mixer_channel_data *channel = &mixer_channel[ch];
  663.  
  664.     mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  665.  
  666.     /* compute the step size for sample rate conversion */
  667.     if (freq != channel->frequency)
  668.     {
  669.         channel->frequency = freq;
  670.         channel->step_size = (UINT32)((double)freq * (double)(1 << FRACTION_BITS) / (double)Machine->sample_rate);
  671.     }
  672. }
  673.  
  674.  
  675. /***************************************************************************
  676.     mixer_sound_enable_global_w
  677. ***************************************************************************/
  678.  
  679. void mixer_sound_enable_global_w(int enable)
  680. {
  681.     int i;
  682.     struct mixer_channel_data *channel;
  683.  
  684.     /* update all channels (for streams this is a no-op) */
  685.     for (i = 0, channel = mixer_channel; i < first_free_channel; i++, channel++)
  686.     {
  687.         mixer_update_channel(channel, sound_scalebufferpos(samples_this_frame));
  688.     }
  689.  
  690.     mixer_sound_enabled = enable;
  691. }
  692.  
  693.  
  694. /***************************************************************************
  695.     mix_sample_8
  696. ***************************************************************************/
  697.  
  698. void mix_sample_8(struct mixer_channel_data *channel, int samples_to_generate)
  699. {
  700.     UINT32 step_size, input_frac, output_pos;
  701.     INT8 *source, *source_end;
  702.     INT32 mixing_volume;
  703.  
  704.     /* compute the overall mixing volume */
  705.     if (mixer_sound_enabled)
  706.         mixing_volume = ((channel->volume * channel->mixing_level * 256) << channel->gain) / (100*100);
  707.     else
  708.         mixing_volume = 0;
  709.  
  710.     /* get the initial state */
  711.     step_size = channel->step_size;
  712.     source = channel->data_current;
  713.     source_end = channel->data_end;
  714.     input_frac = channel->input_frac;
  715.     output_pos = (accum_base + channel->samples_available) & ACCUMULATOR_MASK;
  716.  
  717.     /* an outer loop to handle looping samples */
  718.     while (samples_to_generate > 0)
  719.     {
  720.         /* if we're mono or left panning, just mix to the left channel */
  721.         if (!is_stereo || channel->pan == MIXER_PAN_LEFT)
  722.         {
  723.             while (source < source_end && samples_to_generate > 0)
  724.             {
  725.                 left_accum[output_pos] += *source * mixing_volume;
  726.                 input_frac += step_size;
  727.                 source += input_frac >> FRACTION_BITS;
  728.                 input_frac &= FRACTION_MASK;
  729.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  730.                 samples_to_generate--;
  731.             }
  732.         }
  733.  
  734.         /* if we're right panning, just mix to the right channel */
  735.         else if (channel->pan == MIXER_PAN_RIGHT)
  736.         {
  737.             while (source < source_end && samples_to_generate > 0)
  738.             {
  739.                 right_accum[output_pos] += *source * mixing_volume;
  740.                 input_frac += step_size;
  741.                 source += input_frac >> FRACTION_BITS;
  742.                 input_frac &= FRACTION_MASK;
  743.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  744.                 samples_to_generate--;
  745.             }
  746.         }
  747.  
  748.         /* if we're stereo center, mix to both channels */
  749.         else
  750.         {
  751.             while (source < source_end && samples_to_generate > 0)
  752.             {
  753.                 INT32 mixing_value = *source * mixing_volume;
  754.                 left_accum[output_pos] += mixing_value;
  755.                 right_accum[output_pos] += mixing_value;
  756.                 input_frac += step_size;
  757.                 source += input_frac >> FRACTION_BITS;
  758.                 input_frac &= FRACTION_MASK;
  759.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  760.                 samples_to_generate--;
  761.             }
  762.         }
  763.  
  764.         /* handle the end case */
  765.         if (source >= source_end)
  766.         {
  767.             /* if we're done, stop playing */
  768.             if (!channel->is_looping)
  769.             {
  770.                 channel->is_playing = 0;
  771.                 break;
  772.             }
  773.  
  774.             /* if we're looping, wrap to the beginning */
  775.             else
  776.                 source -= (INT8 *)source_end - (INT8 *)channel->data_start;
  777.         }
  778.     }
  779.  
  780.     /* update the final positions */
  781.     channel->input_frac = input_frac;
  782.     channel->data_current = source;
  783. }
  784.  
  785.  
  786. /***************************************************************************
  787.     mix_sample_16
  788. ***************************************************************************/
  789.  
  790. void mix_sample_16(struct mixer_channel_data *channel, int samples_to_generate)
  791. {
  792.     UINT32 step_size, input_frac, output_pos;
  793.     INT16 *source, *source_end;
  794.     INT32 mixing_volume;
  795.  
  796.     /* compute the overall mixing volume */
  797.     if (mixer_sound_enabled)
  798.         mixing_volume = ((channel->volume * channel->mixing_level * 256) << channel->gain) / (100*100);
  799.     else
  800.         mixing_volume = 0;
  801.  
  802.     /* get the initial state */
  803.     step_size = channel->step_size;
  804.     source = channel->data_current;
  805.     source_end = channel->data_end;
  806.     input_frac = channel->input_frac;
  807.     output_pos = (accum_base + channel->samples_available) & ACCUMULATOR_MASK;
  808.  
  809.     /* an outer loop to handle looping samples */
  810.     while (samples_to_generate > 0)
  811.     {
  812.         /* if we're mono or left panning, just mix to the left channel */
  813.         if (!is_stereo || channel->pan == MIXER_PAN_LEFT)
  814.         {
  815.             while (source < source_end && samples_to_generate > 0)
  816.             {
  817.                 left_accum[output_pos] += (*source * mixing_volume) >> 8;
  818.  
  819.                 input_frac += step_size;
  820.                 source += input_frac >> FRACTION_BITS;
  821.                 input_frac &= FRACTION_MASK;
  822.  
  823.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  824.                 samples_to_generate--;
  825.             }
  826.         }
  827.  
  828.         /* if we're right panning, just mix to the right channel */
  829.         else if (channel->pan == MIXER_PAN_RIGHT)
  830.         {
  831.             while (source < source_end && samples_to_generate > 0)
  832.             {
  833.                 right_accum[output_pos] += (*source * mixing_volume) >> 8;
  834.  
  835.                 input_frac += step_size;
  836.                 source += input_frac >> FRACTION_BITS;
  837.                 input_frac &= FRACTION_MASK;
  838.  
  839.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  840.                 samples_to_generate--;
  841.             }
  842.         }
  843.  
  844.         /* if we're stereo center, mix to both channels */
  845.         else
  846.         {
  847.             while (source < source_end && samples_to_generate > 0)
  848.             {
  849.                 INT32 mixing_value = (*source * mixing_volume) >> 8;
  850.                 left_accum[output_pos] += mixing_value;
  851.                 right_accum[output_pos] += mixing_value;
  852.  
  853.                 input_frac += step_size;
  854.                 source += input_frac >> FRACTION_BITS;
  855.                 input_frac &= FRACTION_MASK;
  856.  
  857.                 output_pos = (output_pos + 1) & ACCUMULATOR_MASK;
  858.                 samples_to_generate--;
  859.             }
  860.         }
  861.  
  862.         /* handle the end case */
  863.         if (source >= source_end)
  864.         {
  865.             /* if we're done, stop playing */
  866.             if (!channel->is_looping)
  867.             {
  868.                 channel->is_playing = 0;
  869.                 break;
  870.             }
  871.  
  872.             /* if we're looping, wrap to the beginning */
  873.             else
  874.                 source -= (INT16 *)source_end - (INT16 *)channel->data_start;
  875.         }
  876.     }
  877.  
  878.     /* update the final positions */
  879.     channel->input_frac = input_frac;
  880.     channel->data_current = source;
  881. }
  882.  
  883.  
  884.